import { hasAuthority } from '@/utils/authority-utils'
// import {loginIgnore} from '@/router/index'
import NProgress from 'nprogress'
import Vue from 'vue'
import { ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types'
import store from '../store'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/user/register', '/user/register-result', '/user/alteration'] // no redirect whitelist

// /**
//  * 进度条开始
//  * @param to
//  * @param form
//  * @param next
//  */
// const progressStart = (to, from, next) => {
//   // start progress bar
//   if (!NProgress.isStarted()) {
//     NProgress.start()
//   }
//   next()
// }
//
// /**
//  * 登录守卫
//  * @param to
//  * @param form
//  * @param next
//  * @param options
//  */
// const loginGuard = (to, from, next, options) => {
//   console.log('loginGuard', to, from)
//   const {message} = options
//   if (!loginIgnore.includes(to) && !Vue.ls.get(ACCESS_TOKEN)) {
//     message.warning('登录已失效，请重新登录')
//     next({path: '/login'})
//   } else {
//     next()
//   }
// }
//
// /**
//  * 权限守卫
//  * @param to
//  * @param form
//  * @param next
//  * @param options
//  */
// const authorityGuard = (to, from, next, options) => {
//   console.log('authorityGuard', to, from)
//   const token = Vue.ls.get(ACCESS_TOKEN)
//   if (token){
//     const {store, message} = options
//     console.log(store.getters.roles)
//     // const permissions = store.getters['account/permissions']
//     let roles = store.getters.roles
//     if (roles.length === 0) {
//       store.dispatch('GetPermissionList').then(()=> {
//         roles = store.getters.roles
//         if (!hasAuthority(to,roles)) {
//           message.warning(`对不起，您无权访问页面: ${to.fullPath}，请联系管理员`)
//           next({path: '/403'})
//           // NProgress.done()
//         } else {
//           next()
//         }
//       })
//     } else {
//       if (!hasAuthority(to, roles)) {
//         message.warning(`对不起，您无权访问页面: ${to.fullPath}，请联系管理员`)
//         next({path: '/403'})
//         // NProgress.done()
//       } else {
//         next()
//       }
//     }
//   } else {
//     next()
//   }
// }
//
// /**
//  * 混合导航模式下一级菜单跳转重定向
//  * @param to
//  * @param from
//  * @param next
//  * @param options
//  * @returns {*}
//  */
// const redirectGuard = (to, from, next, options) => {
//   console.log('redirectGuard', to, from)
//   const {store} = options
//   const getFirstChild = (routes) => {
//     const route = routes[0]
//     if (!route.children || route.children.length === 0) {
//       return route
//     }
//     return getFirstChild(route.children)
//   }
//   if (store.state.setting.layout === 'mix') {
//     const firstMenu = store.getters['setting/firstMenu']
//     if (firstMenu.find(item => item.fullPath === to.fullPath)) {
//       store.commit('setting/setActivatedFirst', to.fullPath)
//       const subMenu = store.getters['setting/subMenu']
//       if (subMenu.length > 0) {
//         const redirect = getFirstChild(subMenu)
//         return next({path: redirect.fullPath})
//       }
//     }
//   }
//   next()
// }

const beforeEach = (to, from, next) => {
  NProgress.start() // start progress bar
  console.log('beforeEach', to, from)
  if (Vue.ls.get(ACCESS_TOKEN)) {
    /* has token */
    if (to.path === '/login') {
      next({ path: INDEX_MAIN_PAGE_PATH })
      NProgress.done()
    } else {
      let roles = store.getters.roles
      if (roles.length === 0) {
        store.dispatch('GetPermissionList').then(() => {
          roles = store.getters.roles
          if (!hasAuthority(to, roles)) {
            // message.warning(`对不起，您无权访问页面: ${to.fullPath}，请联系管理员`)
            next({ path: '/403' })
            // NProgress.done()
          } else {
            next()
          }
        }).catch((err) => {
            /* notification.error({
               message: '系统提示',
               description: '请求用户信息失败，请重试！'
             }) */
          console.error(err)
            store.dispatch('Logout').then(() => {
              next({ path: '/login', query: { redirect: to.fullPath } })
            })
          })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next({ path: '/login', query: { redirect: to.fullPath } })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
}

/**
 * 进度条结束
 * @param to
 * @param form
 * @param options
 */
const progressDone = () => {
  console.log('progressDone')
  // finish progress bar
  NProgress.done()
}

export default {
  beforeEach: [beforeEach],
  afterEach: [progressDone]
}
